package com.cloud.job.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cloud.common.core.model.response.ApiResponses;
import com.cloud.common.core.model.supers.SuperController;
import com.cloud.common.security.annotation.PreAuth;
import com.cloud.job.project.model.entity.QuartzJobLog;
import com.cloud.job.project.model.parm.QuartzJobLogPARM;
import com.cloud.job.project.service.IQuartzJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 定时任务调度日志表 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-02-08
 */
@Api(tags = {"定时任务调度日志"})
@RestController
@RequestMapping("/quartzJobLog")
public class QuartzJobLogRestController extends SuperController {

    @Autowired
    private IQuartzJobLogService quartzJobLogService;

    @PreAuth
    @ApiOperation("分页查询定时任务调度日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobName", value = "任务名称 模糊", paramType = "query"),
            @ApiImplicitParam(name = "jobGroup", value = "任务组名", paramType = "query"),
            @ApiImplicitParam(name = "invokeTarget", value = "调用目标字符串 模糊", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "用户状态", type = "int", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "停止时间", paramType = "query"),
    })
    @GetMapping
    public ApiResponses<IPage<QuartzJobLog>> page(@ApiIgnore @ModelAttribute QuartzJobLogPARM quartzJobLogPARM) {
        IPage<QuartzJobLog> iPage = quartzJobLogService.page(this.getPage(), Wrappers.<QuartzJobLog>lambdaQuery()
                .select(QuartzJobLog.class, info -> !StringUtils.equalsAny(info.getColumn(), "exception_info"))
                .like(StringUtils.isNotBlank(quartzJobLogPARM.getJobName()), QuartzJobLog::getJobName, quartzJobLogPARM.getJobName())
                .eq(StringUtils.isNotBlank(quartzJobLogPARM.getJobGroup()), QuartzJobLog::getJobGroup, quartzJobLogPARM.getJobGroup())
                .like(StringUtils.isNotBlank(quartzJobLogPARM.getInvokeTarget()), QuartzJobLog::getInvokeTarget, quartzJobLogPARM.getInvokeTarget())
                .eq(Objects.nonNull(quartzJobLogPARM.getStatus()), QuartzJobLog::getStatus, quartzJobLogPARM.getStatus())
                .gt(Objects.nonNull(quartzJobLogPARM.getStartTime()), QuartzJobLog::getCreateTime, quartzJobLogPARM.getStartTime())
                .lt(Objects.nonNull(quartzJobLogPARM.getEndTime()), QuartzJobLog::getCreateTime, quartzJobLogPARM.getEndTime()));
        return success(iPage);
    }

    @PreAuth
    @ApiOperation("单条查询任务调度日志")
    @GetMapping(value = "/{jobLogId}")
    public ApiResponses<QuartzJobLog> getById(@PathVariable Long jobLogId) {
        return success(quartzJobLogService.getById(jobLogId));
    }

    @PreAuth
    @ApiOperation("删除定时任务调度日志")
    @DeleteMapping("/{jobLogIds}")
    public ApiResponses<Void> deleteByIds(@PathVariable List<Long> jobLogIds) {
        quartzJobLogService.removeByIds(jobLogIds);
        return success();
    }

    @PreAuth
    @ApiOperation("清空定时任务调度日志")
    @DeleteMapping("/clean")
    public ApiResponses<Void> clean() {
        quartzJobLogService.cleanJobLog();
        return success();
    }

}

